<?php
namespace app\admin\controller;
use catetree\Catetree;
class Pk extends Common
{
    public function lst()
    {
        $articleRes=db('pk')->order('id desc')->select();
        $count=db('pk')->count();
        
        foreach ($articleRes as $k=>$v){
            if($v['cats']){
                $cat_Arr = explode(',', $v['cats']);
                $articleRes[$k]['cat_names'] = '';
                foreach ($cat_Arr as $k1=>$v1){
                    if($k1!==0){
                        $articleRes[$k]['cat_names'] .='&nbsp;&nbsp;'.db('pk_group')->where('id',$v1)->value('cat_name');
                    }else{
                        $articleRes[$k]['cat_names'] .=db('pk_group')->where('id',$v1)->value('cat_name');
                    }
                }
            }else{
                $articleRes[$k]['cat_names']='';
            }
        }
        $aid = session('uid','','admin');
        $group_id=db('admin')->where('id',$aid)->value('group_id');
        $this->assign([
            'articleRes'=>$articleRes,
            'count'=>$count,
            'group_id'=>$group_id
        ]);
        return view();
    }

    public function add()
    {
        if(request()->isPost()){
            $data=input('post.');
            $cats = implode(',', $data['cats']);
            $data = $data['data'];
            $data['cats'] = $cats;
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            $data['add_time']=time();
            $add=db('pk')->insert($data);
            if($add){
                return ['msg'=>'新增活动成功','icon'=>1];
            }else{
                return ['msg'=>'新增活动失败','icon'=>2];
            }
            return;
        }
        $cateRes=db('pk_group')->order('level')->order('id')->select();
        foreach ($cateRes as $k=>$v){
            if($v['admins']){
                $admin_Arr = explode(',', $v['admins']);
                $cateRes[$k]['admin_names'] = '';
                foreach ($admin_Arr as $k1=>$v1){
                    if($k1!==0){
                        $cateRes[$k]['admin_names'] .='&nbsp;&nbsp;'.db('admin')->where('id',$v1)->value('name');
                    }else{
                        $cateRes[$k]['admin_names'] .=db('admin')->where('id',$v1)->value('name');
                    }
                }
            }else{
                $cateRes[$k]['admin_names']='';
            }
        }
        $this->assign('cateRes',$cateRes);

        return view();
    }

    public function edit()
    {
        if(request()->isPost()){
            $data=input('post.');
            $cats = implode(',', $data['cats']);
            $data = $data['data'];
            $data['cats'] = $cats;
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            
            $save=db('pk')->update($data);
            if($save !== false){
                return ['msg'=>'编辑活动成功','icon'=>1];
            }else{
                return ['msg'=>'编辑活动失败','icon'=>2];
            }
            return;
        }
        $art_id=input('id');
        $articles=db('pk')->find($art_id);
        $articles['start_time'] = date("Y-m-d H:i:s",$articles['start_time']);
        $articles['end_time'] = date("Y-m-d H:i:s",$articles['end_time']);
        $cateRes=db('pk_group')->order('level')->order('id')->select();
        foreach ($cateRes as $k=>$v){
            if($v['admins']){
                $admin_Arr = explode(',', $v['admins']);
                $cateRes[$k]['admin_names'] = '';
                foreach ($admin_Arr as $k1=>$v1){
                    if($k1!==0){
                        $cateRes[$k]['admin_names'] .='&nbsp;&nbsp;'.db('admin')->where('id',$v1)->value('name');
                    }else{
                        $cateRes[$k]['admin_names'] .=db('admin')->where('id',$v1)->value('name');
                    }
                }
            }else{
                $cateRes[$k]['admin_names']='';
            }
        }
        $cats_Arr = explode(',', $articles['cats']);
        $this->assign([
            'articles'=>$articles,
            'cateRes'=>$cateRes,
            'cats_Arr'=>$cats_Arr
        ]);
        return view();
    }

    public function del()
    {
        $art_id=input('id');
        //删除数据库记录
        $del=db('pk')->delete($art_id);
        if($del){
            return ['msg'=>'删除活动成功','icon'=>1];
        }else{
            return ['msg'=>'删除活动失败','icon'=>2];
        }
    }
    public function paihang()
    {
        $id=input('id');
        $articles = db('pk')->find($id);
        $cats_Arr = explode(',', $articles['cats']);
        
        foreach ($cats_Arr as $k=>$v){
            $cats_Arr[$k]=array();

            $cats_Arr[$k]=db('pk_group')->where('id',$v)->find();
            $admin_Arr = explode(',', $cats_Arr[$k]['admins']);
            $count = 0;
            $qiandan = 0;
            $count_fen = 0;
            $admins_name = '';
            foreach ($admin_Arr as $k1=>$v1){
                /*录入客户量*/
               $jia_count=db('client')->where('aid',$v1)->where('add_time','>',$articles['start_time'])->where('add_time','<=',$articles['end_time'])->count();
               $count+=$jia_count;
               $jia_count_fen = $jia_count * $articles['luru_fen'];
               $count_fen+=$jia_count_fen;
               /*签单客户量*/
               $jia_qiandan = db('client')->where('status','4')->where('xaid',$v1)->where('qiandan_time','>',$articles['start_time'])->where('qiandan_time','<=',$articles['end_time'])->count();
               $qiandan+=$jia_qiandan;
               $jia_qiandan_fen = $jia_qiandan * $articles['qiandan_fen'];
               $count_fen+=$jia_qiandan_fen;
               /*组内成员*/
               if($k1 == 0){
                   $admins_name.= db('admin')->where('id',$v1)->value('name');
               }else{
                   $name = db('admin')->where('id',$v1)->value('name');
                   $admins_name.= ','.$name;
               }
               
            }
            $cats_Arr[$k]['count'] = $count;
            $cats_Arr[$k]['qiandan'] = $qiandan;
            $cats_Arr[$k]['count_fen'] = $count_fen;
            $cats_Arr[$k]['admins_name'] = $admins_name;
        }

        array_multisort(array_column($cats_Arr,'count_fen'),SORT_DESC,$cats_Arr);
        $this->assign([
            'articles'=>$articles,
            'cats_Arr'=>$cats_Arr
        ]);
        return view();
    }

    // //layui图片上传
    // public function upload(){
    //     $file = request()->file('file');
    //     // 移动到框架应用根目录/public/uploads/ 目录下
    //     $info = $file->move(ARTIMG);
    //     $data = array();  //定义一个返回的数组
    //     if($info){
    //         $reubfo['code']= 1;
    //         $reubfo['savename'] = $info->getSaveName(); 
    //     }else{
    //         // 上传失败获取错误信息
    //         $reubfo['code']= 0;
    //         $reubfo['err'] = $file->getError();
    //     } 
    //     return json_encode($reubfo);
    // }

    // //ajax栏目添加修改页面图片撤销功能 删除本地图片
    // public function cancel(){
    //     $art_id=input('art_id');
    //     $imgurl=input('imgurl');
    //     $res=@unlink(ARTIMG.$imgurl);
    //     if($art_id){
    //         db('pk')->where('id',$art_id)->setField('art_img','');
    //     }
    //     if($res){
    //         echo 1;
    //     }else{
    //         echo 2;
    //     }
    // }
    // //ajax更新开启状态
    // public function changeStatus()
    // {
    //     if(request()->isAjax()){
    //         $id=input('id');
    //         $status=input('status');
    //         if($status == 1){
    //             db('pk')->where(['id'=>$id])->update(['status'=>0]);
    //             return ['msg'=>'已隐藏','icon'=>2];
    //         }else{
    //             db('pk')->where(['id'=>$id])->update(['status'=>1]);
    //             return ['msg'=>'已显示','icon'=>1];
    //         }
    //     }else{
    //         $this->error("非法操作！");
    //     }
    // }


}
